package com.example.miaopin.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.media.ThumbnailUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class RoundView extends TextView {
	Canvas canvas;
	float angle1 = -90, angle2 = -90, outSize, radius, offset;
	int[] ringColor = { 0xFF55ffff, 0xFF887700 };
	int[] lineWidth = { 13, 8 };
	// Bitmap bmp = null, bmp_out = null;
	Paint[] ringPaint;
	// Paint bmpPaint;
	int height, width;
	// int bmpHeight, bmpWidth;
	// boolean bmpIsChange = false;
	RectF rf0, rf1, rf2;
	float[] foot = { 1f, -1f };

	public RoundView(Context context) {
		super(context);
		// bmpPaint = new Paint();
		// bmpPaint.setAntiAlias(true);
	}

	public RoundView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// bmpPaint = new Paint();
		// bmpPaint.setAntiAlias(true);
	}

	public RoundView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// bmpPaint = new Paint();
		// bmpPaint.setAntiAlias(true);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		angle1 = angle1 < 0 ? angle1 + 360 : angle1 > 360 ? angle1 - 360 : angle1;
		angle2 = angle2 < 0 ? angle2 + 360 : angle2 > 360 ? angle2 - 360 : angle2;
		this.canvas = canvas;
		height = getHeight();
		width = getWidth();
		// height = canvas.getHeight();
		// width = canvas.getWidth();
		radius = canvas.getHeight() > canvas.getWidth() ? (((float) width) - 10) / 2 : (((float) height) - 10) / 2;
		outSize = radius * 2;

		if (rf1 == null) {
			if (height > width) {
				offset = (((float) height) - outSize) / 2;
				rf0 = new RectF(10, offset + 10, outSize + 0, offset + outSize + 0);
				rf1 = new RectF(12, offset + 12, outSize + -2, offset + outSize + -2);
				rf2 = new RectF(6, offset + 6, outSize + 4, offset + outSize + 4);
			} else {
				offset = (((float) width) - outSize) / 2;
				rf0 = new RectF(offset + 10, 10, offset + outSize + 0, outSize + 0);
				rf1 = new RectF(offset + 12, 12, offset + outSize + -2, outSize + -2);
				rf2 = new RectF(offset + 6, 6, offset + outSize + 4, outSize + 4);
			}
		}
		// if(this.bmp != null){
		// if(this.bmpIsChange){
		// reSize();
		//
		// bmpHeight = bmp.getHeight();
		// bmpWidth = bmp.getWidth();
		// //插算
		// float ratio = ((float)outSize) / (float)(bmpHeight > bmpWidth ?
		// bmpWidth : bmpHeight) ;
		// int outHeight = (int) (ratio * bmpHeight)
		// , outWidth = (int) (ratio * bmpWidth);
		// bmp_out = ThumbnailUtils.extractThumbnail(bmp, outWidth, outHeight);
		// //切方图
		// int out_offset;
		//// Log.i("AndroidRuntime","outHeight:outWidth=" + outHeight +":"+
		// outWidth);
		// if(outWidth > outHeight){
		// out_offset = (outWidth - outHeight) /2;
		// bmp_out = Bitmap.createBitmap(bmp_out, out_offset, 0, outHeight +
		// out_offset, outHeight);
		// }else{
		// out_offset = (outHeight - outWidth) /2;
		// bmp_out = Bitmap.createBitmap(bmp_out, 0, out_offset, outWidth,
		// outWidth + out_offset);
		// }
		// Paint roundPaint;
		// roundPaint = new Paint();
		// roundPaint.setAntiAlias(true);
		// //圆图
		// Bitmap tar =
		// Bitmap.createBitmap((int)outSize,(int)outSize,Config.ARGB_8888);
		// Canvas cv = new Canvas(tar);
		// cv.drawCircle(((float)outSize) / 2, ((float)outSize) / 2,
		// ((float)outSize) / 2, roundPaint);
		// roundPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		// cv.drawBitmap(bmp_out,0,0,roundPaint);
		// bmp_out = tar;
		//
		// canvas.drawBitmap(bmp_out, new Rect(0,0,(int)outSize,(int)outSize),
		// rf0, bmpPaint);
		// }
		// }
		// 圆环
		if (ringPaint == null) {
			ringPaint = new Paint[2];
			for (int i = 0; i < 2; i++) {
				ringPaint[i] = new Paint();
				ringPaint[i].setStyle(Paint.Style.STROKE);
				ringPaint[i].setAntiAlias(true);
			}
		}
		for (int i = 0; i < 2; i++) {
			ringPaint[i].setStrokeWidth(lineWidth[i]);
			ringPaint[i].setColor(this.ringColor[i]);
		}
		drawRing();
		// canvas.drawArc(rf1, angle1++, 340, false, ringPaint[0]);
		// canvas.drawArc(rf2, angle2--, 250, false, ringPaint[1]);
		angle1 += foot[0];
		angle2 += foot[1];
		invalidate();
	}

	// public void setBmp(Bitmap bmp){
	// this.bmp = bmp;
	// bmpIsChange = true;
	// }
	public void setRingColor(int color1, int color2) {
		this.ringColor[0] = color1;
		this.ringColor[1] = color2;
		this.ringPaint[0].setColor(color1);
		this.ringPaint[1].setColor(color2);
	}

	public void setLineWidth(int width1, int width2) {
		this.lineWidth[0] = width1;
		this.lineWidth[1] = width2;
	}

	public void setFoot(float foot1, float foot2) {
		this.foot[0] = foot1;
		this.foot[1] = foot2;
	}

	public void reSize() {
		height = getHeight();
		width = getWidth();
		// height = canvas.getHeight();
		// width = canvas.getWidth();
		radius = canvas.getHeight() > canvas.getWidth() ? (((float) width) - 10) / 2 : (((float) height) - 10) / 2;
		outSize = radius * 2;

		if (rf1 == null) {
			if (height > width) {
				offset = (((float) height) - outSize) / 2;
				rf0 = new RectF(10, offset + 10, outSize + 0, offset + outSize + 0);
				rf1 = new RectF(12, offset + 12, outSize + -2, offset + outSize + -2);
				rf2 = new RectF(6, offset + 6, outSize + 4, offset + outSize + 4);
			} else {
				offset = (((float) width) - outSize) / 2;
				rf0 = new RectF(offset + 10, 10, offset + outSize + 0, outSize + 0);
				rf1 = new RectF(offset + 12, 12, offset + outSize + -2, outSize + -2);
				rf2 = new RectF(offset + 6, 6, offset + outSize + 4, outSize + 4);
			}
		}
		// bmpIsChange = true;
	}

	// private void drawRound(){
	//// Log.i("AndroidRuntime","canvas=" + canvas);
	//// Log.i("AndroidRuntime","bmp_out" + bmp_out);
	//// Log.i("AndroidRuntime","outSize=" + outSize);
	//// Log.i("AndroidRuntime","rf0=" + rf0);
	//// Log.i("AndroidRuntime","bmpPaint=" + bmpPaint);
	//
	// this.canvas
	// .drawBitmap(bmp_out, new Rect(0,0,(int)outSize,(int)outSize), rf0,
	// bmpPaint);
	// }
	private void drawRing() {
		canvas.drawArc(rf1, angle1, 340, false, ringPaint[0]);
		canvas.drawArc(rf2, angle2, 250, false, ringPaint[1]);
	}
	// private void calcuateBmp(){
	// bmpHeight = bmp.getHeight();
	// bmpWidth = bmp.getWidth();
	// //插算
	// float ratio = ((float)outSize) / (float)(bmpHeight > bmpWidth ? bmpWidth
	// : bmpHeight) ;
	// int outHeight = (int) (ratio * bmpHeight)
	// , outWidth = (int) (ratio * bmpWidth);
	// bmp_out = ThumbnailUtils.extractThumbnail(bmp, outWidth, outHeight);
	// //切方图
	// int out_offset;
	//// Log.i("AndroidRuntime","outHeight:outWidth=" + outHeight +":"+
	// outWidth);
	// if(outWidth > outHeight){
	// out_offset = (outWidth - outHeight) /2;
	// bmp_out = Bitmap.createBitmap(bmp_out, out_offset, 0, outHeight +
	// out_offset, outHeight);
	// }else{
	// out_offset = (outHeight - outWidth) /2;
	// bmp_out = Bitmap.createBitmap(bmp_out, 0, out_offset, outWidth, outWidth
	// + out_offset);
	// }
	//// bmp = Bitmap.createBitmap(bmp, 0, 0, outWidth, outHeight);
	// Paint roundPaint;
	// roundPaint = new Paint();
	// roundPaint.setAntiAlias(true);
	// //圆图
	// Bitmap tar =
	// Bitmap.createBitmap((int)outSize,(int)outSize,Config.ARGB_8888);
	// Canvas cv = new Canvas(tar);
	// cv.drawCircle(((float)outSize) / 2, ((float)outSize) / 2,
	// ((float)outSize) / 2, roundPaint);
	// roundPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	// cv.drawBitmap(bmp_out,0,0,roundPaint);
	// bmp_out = tar;
	// }

}